# -*- autoconf -*-
#
# generate empty files
#
# Create include file list for snmp_vars.h to use.
mibdir="agent/mibgroup"
if test ! -d agent; then
  mkdir agent
fi
if test ! -d agent/mibgroup; then
  mkdir agent/mibgroup
fi
j="$mibdir/mib_module_includes.h  $mibdir/agent_module_includes.h \
   $mibdir/mib_module_inits.h     $mibdir/agent_module_inits.h    \
   $mibdir/mib_module_shutdown.h  $mibdir/agent_module_shutdown.h \
   include/net-snmp/agent/mib_module_config.h                     \
                   include/net-snmp/agent/agent_module_config.h   \
   $mibdir/mib_module_dot_conf.h  $mibdir/agent_module_dot_conf.h"

for i in $j; do
  rm -f $i
  echo "/* This file is automatically generated by configure.  Do not modify by hand. */" > $i
done

#
# Do hairy agent module processing.
#
AC_MSG_CHECKING([for and configuring mib modules to use])

AH_TOP([#define NETSNMP_REQUIRE_SEMICOLON struct netsnmp_unused_struct])

# set up the CPP command
MODULECPP="$CPP $PARTIALTARGETFLAGS $CPPFLAGS -DNETSNMP_FEATURE_CHECKING -I${srcdir}/include -I${srcdir}/agent/mibgroup"
if test "x$enable_mfd_rewrites" = "xyes"; then
  MODULECPP="$MODULECPP -DNETSNMP_ENABLE_MFD_REWRITES"
fi

module_list=""
mib_module_list=""
agent_module_list=""
new_with_mib_modules=`echo $with_mib_modules | $SED 's/,/ /g;'`
new_with_out_mib_modules=`echo $with_out_mib_modules | $SED 's/,/ /g;'`

# set up initial module list
new_module_list="$new_with_mib_modules"

if test "x$enable_agent" != "xno" ; then
  # If the agent should be built then use default_modules
  new_module_list="$new_module_list default_modules"

  if test "x$mini_agent" = "xyes"; then
    # define the mini agent flag to reduce our load list from default_mibs.h
    MODULECPP="$MODULECPP -DNETSNMP_MINI_AGENT"
  fi
elif test "x$enable_applications" != "xno" ; then
    # otherwise, if snmptrapd should be built then add SNMPv3 and
    # snmptrapd-specific parts
    new_module_list="$new_module_list mibII/vacm_conf utilities/execute"
    default_mibs=SNMPv2-MIB:IF-MIB:IP-MIB:TCP-MIB:UDP-MIB
    default_mibs_install=" "
fi

test_modules="$new_module_list"
new_module_list=""

# remove modules that were explicitly excluded
for i in $test_modules
 do
  if echo " $new_with_out_mib_modules " | $GREP " $i " > /dev/null; then
    mib_cfg_msg="mib module '$i' is both included and excluded."
    AC_MSG_WARN([mib module conflict])
    if test "x$with_mib_cfg_checks" = "xyes"; then
      AC_MSG_WARN([user with    : $with_mib_modules])
      AC_MSG_WARN([final with   : $new_module_list])
      AC_MSG_WARN([user without : $with_out_mib_modules])
      AC_MSG_WARN([final without: $new_with_out_mib_modules])
      AC_MSG_ERROR([$mib_cfg_msg])
    else
      AC_MSG_WARN([$mib_cfg_msg It will be excluded])
      AC_MSG_CACHE_ADD([*** MIB Module warning *** $mib_cfg_msg It has been excluded.])
    fi
  else
    new_module_list="$new_module_list $i"
  fi
done

new_module_list=`(echo $new_module_list | tr ' ' '\n' | sort | tr '\n' ' '; echo '') | $SED 's/ $//'`

AC_MSG_RESULT([$new_module_list.])

#---------------------
# loop over module list
#
all_warnings=""
if test "x$with_mib_cfg_debug" = "xyes"; then
   module_debug=1
else
   module_debug=0
fi
first_pass=1
output_to=normal

if test ! -d mk ; then
  mkdir mk
fi


module_rules=mk/module_rules.mk
echo "# contents below built automatically by configure; do not edit by hand" > $module_rules

#
# make file sub-pieces
#
for i in module_list_deps mib_module_list_deps agent_module_list_deps ; do
  eval "$i=mk/${i}.mk"
  echo "# contents below built automatically by configure; do not edit by hand" > mk/${i}.mk
done

#
# variable settings sub-pieces
#
for i in module_list_code mib_module_list_code agent_module_list_code module_list mib_module_list agent_module_list ; do
  eval "$i=mk/${i}.mk"
  echo "# contents below built automatically by configure; do not edit by hand" > mk/${i}.mk
  echo "$i= \\" >> mk/${i}.mk
done

echo $ECHO_N "echo " $ECHO_C > mk/MODULE_LIST.mk
MODULE_LIST=mk/MODULE_LIST.mk


#---------------------
# until we have an empty module list...
#   (modules may add new modules, and until all dependencies are done...)
#
while test "x$new_module_list" != "x"; do
  AC_MSG_MODULE_DBG(-e,"\n****************************************************************")
  AC_MSG_MODULE_DBG("nml: $new_module_list")

  #---------------------
  # loop over all the modules in the current list
  #
  for j in $new_module_list
    do

    #---------------------
    # figure out which sub-blocks to build in parts
    if echo "$j" | $GREP "=" ; then
       # build a shared object file
       # format: sofilename=module1:module2
       # XXX: currently doesn't do this
       dl_name=`echo $j | $SED 's/=.*//'`
       dl_names="$dl_names $dl_name"
       dl_objects=`echo $j | $SED 's/.*=//'`
       module_sublist=`echo $dl_objects | $SED 's/:/ /g'`
       saved_sublist=""
       saved_sublist_code=""
       output_to=separate

       # create output files
       # these actually get nuked later
       output_inits="agent/mibgroup/${dl_name}-dll.c"
       output_hdrs="agent/mibgroup/${dl_name}-hdrs.h"
    else
       # not a special token invocation
       module_sublist=$j
       output_to=normal
       output_t=
    fi

    while test "x$module_sublist" != "x" ; do

    #---------------------
    # loop over all the modules in the sub list
    for i in $module_sublist
    do

    AC_MSG_MODULE_DBG(-e "\n================================================================")
    AC_MSG_MODULE_DBG("testing $i")
    need_header=0

    #---------------------
    # build a pseudo include header that includes current config and
    # system symbols:
    rm -f conftest.$ac_ext module_tmp_header.i
    AC_LANG_CONFTEST([AC_LANG_SOURCE([[
#include NETSNMP_SYSTEM_INCLUDE_FILE
#include "$srcdir/$mibdir/$i.h"]])])
    $MODULECPP conftest.$ac_ext >module_tmp_header.i 2>/dev/null

    #---------------------
    # macro: config_belongs_in(ARG)
    #   ARG: either
    #     - mib_module   => libnetsnmpmibs   (default)
    #     - agent_module => libnetsnmpagent
    #
    AH_TOP([#define config_belongs_in(x) NETSNMP_REQUIRE_SEMICOLON])
    module_type=mib_module
    if test -f $srcdir/$mibdir/$i.h; then
      changequote(, )
      module_type=`cat module_tmp_header.i | \
                   $SED -n 's@.*config_belongs_in(\([^)]*\)).*@\1@p'`
      changequote([, ])
    fi
    if test "x$module_type" = "x" ; then
      module_type=mib_module
    fi
    if test "x$output_to" = "xnormal" ; then
     	output_inits="$mibdir/${module_type}_inits.h"
     	output_hdrs="$mibdir/${module_type}_includes.h"
    fi

    #--------------------
    # check for duplicates within the dll system. 
    #   - if found in a dll, then remove from the dll and add to main
    dblfix=""
    for dltest in $dl_names ; do
      eval mod_files="\"\$dl_${dltest}_files\""
      if echo " $mod_files " | $GREP " $i " > /dev/null; then
        # some dll module has already seen this, unload it from that list
        # and include it in the core (again)
        AC_MSG_MODULE_DBG("removing $i from $dltest")
        mod_files=`echo " $mod_files " | $SED "s@ $i @@"`
        dblfix="__XXX__"
        eval "dl_${dltest}_files=\"$mod_files\""

        module_list="$module_list $i"
	echo "read_config.lo: mibgroup/$i.h" >> $module_list_deps
	if test $module_type = "agent_module" ; then
 	    echo "snmp_vars.lo: mibgroup/$i.h" >> $agent_module_list_deps
        else
 	    echo "mib_modules.lo: mibgroup/$i.h" >> $mib_module_list_deps
        fi
        eval ${module_type}_list="\"\$${module_type}_list $i\""
	echo "	$i \\" >> $module_list_code
	echo $ECHO_N " $i" $ECHO_C >> $MODULE_LIST
        eval tmpfilevar=\$`echo ${module_type}_list_code`
        echo "	$i \\" >> $tmpfilevar

	# XXX: add this to the main init list; this is possible not
	# quite in the right spot any longer but we're stuck with it.
	# Adding it back to the original ordered spot would be a
	# nightmare.  Ok, we're already in nightmare code so that
	# wolud have to be a night-terror.
        mnm=`echo $i | $SED 's/.*\///;s/\.c$//;'`
	output_inits_tmp="$mibdir/${module_type}_inits.h"
	echo "  if (should_init(\"$mnm\")) init_$mnm();" >> $output_inits_tmp
        # XXX: even worse, we loose whether the header is needed or not.
     	output_hdrs_tmp="$mibdir/${module_type}_includes.h"
        echo '#include "'"mibgroup/$i.h"'"' >> $output_hdrs_tmp
      fi
    done

    #---------------------
    # make sure module wasn't explicitly excluded, or already added
    #
    if test "x$dblfix" != "x" ; then
      AC_MSG_MODULE_DBG("$i was duplicated -- putting into main")
    elif echo " $new_with_out_mib_modules " | $GREP " $i " > /dev/null; then
      AC_MSG_MODULE_DBG("$i was excluded")
    elif echo " $module_list " | $GREP " $i " > /dev/null; then
      AC_MSG_MODULE_DBG("$i already tested")
    else

      #---------------------
      # process modules header, checking for config_ pseudo-macros
      #

      #---------------------
      # does the header file for the module exist?
      #   macros are only processed if it does
      if test -f $srcdir/$mibdir/$i.h; then
        new_list=""

        #---------------------
      	# macro: config_exclude(modules)
      	#  - lists conflicts with other modules
      	#    - EG: new MIB implementations exclude old and vice-versa
      	#
        # check if $i has any conflicts
        #
        AH_TOP([#define config_exclude(x) NETSNMP_REQUIRE_SEMICOLON])
        new_list_excl=`cat module_tmp_header.i | \
                       $SED -n 's/.*config_exclude(\(.*\)).*/\1/p'`
	if test "x$new_list_excl" != "x"; then
          AC_MSG_MODULE_DBG("$i excludes $new_list_excl")
	  for j in $new_list_excl
	    do
	    if echo " $module_list $new_mib_module_list $new_list_two " | $GREP " $j " > /dev/null; then
              mib_cfg_msg="mib module '$i' excludes module '$j' but another module requires it"
              AC_MSG_WARN([mib module error])
              if test "x$with_mib_cfg_checks" = "xyes"; then
	        AC_MSG_ERROR([$mib_cfg_msg])
              else
	        AC_MSG_WARN([$mib_cfg_msg])
                AC_MSG_CACHE_ADD([*** MIB Module warning *** $mib_cfg_msg])
              fi
	    else
	      new_with_out_mib_modules="$j $new_with_out_mib_modules"
              AC_MSG_MODULE_DBG("$j added to excluded list")
	    fi
	  done
	fi

        #---------------------
      	# macro: config_arch_require(ARCH,modules)
      	#  - lists modules required for a given ARCH
        #
        # check if $i has any architecture specific requirements
        #
        AH_TOP([#define config_arch_require(x,y) NETSNMP_REQUIRE_SEMICOLON])
        changequote(, )
        new_list_arch=`cat module_tmp_header.i | \
                       $SED -n 's/.*config_arch_require( *\([^ ]*\) *, *\([^ ]*\) *).*/\1-xarchx-\2/p'`
        changequote([, ])
	if test "x$new_list_arch" != "x"; then
          for j in $new_list_arch
	    do
	    archtest=`echo $j | $SED 's/-xarchx-.*//'`
	    # target_os may have been changed to a #define of 1...
	    if test "x$archtest" = "x1" -o "x$target_os" = "x$archtest" -o "x$PARTIALTARGETOS" = "x$archtest"; then
	      new_list="$new_list `echo $j | $SED 's/.*-xarchx-//'`"
              AC_MSG_MODULE_DBG(" $i required adding arch files: `echo $j | $SED 's/.*-xarchx-//'`")
	    fi
	  done
	fi
        AC_MSG_MODULE_DBG(" $i required arch files: $new_list")

        #---------------------
      	# macro: config_version_require((base, version, version-modules, ...))
      	#  - lists alternative modules used from different versions.
        #
        AH_TOP([#define config_version_require(x) NETSNMP_REQUIRE_SEMICOLON])
        [new_list_alt3=`cat module_tmp_header.i | \
            $AWK '
                BEGIN {
                    if("'"$enable_new_features"'" == "yes")
                        method="max";
                    else if("'"$enable_old_features"'" == "yes")
                        method="max-less";
                    else
                        method="max-less-equal";
                    FS = ".";
                    split("'"$with_features_of"'", a);
                    version=sprintf("%03d%03d%03d%03d", a[1], a[2], a[3], a[4]);
                }
                /config_version_require/ {
                    gsub("^.*config_version_require *\\\\(\\\\(", "");
                    gsub("\\\\)\\\\).*$", "");
                    gsub(", *", ",");
                    FS = ",";
                    n = split($0, a);
                    FS = ".";
                    actver = "000000000000";
                    actstr = a[1];
                    for(i = 2; i < n; i += 2) {
                        split(a[i], b);
                        curver=sprintf("%03d%03d%03d%03d", b[1], b[2], b[3], b[4]);
                        if (curver > actver && (method != "max-less" || curver < version) && (method != "max-less-equal" || curver <= version)) {
                            actver = curver;
                            actstr = a[i + 1];
                        }
                    }
                    printf(" %s", actstr);
                }'`]
        AC_MSG_MODULE_DBG(" $i required versioned features: $new_list_alt3")
        new_list="${new_list}${new_list_alt3}"

        #---------------------
      	# macro: config_require(modules)
      	#  - lists modules required for this module
        #
        # check if $i has any other required modules
        #
        AH_TOP([#define config_require(x) NETSNMP_REQUIRE_SEMICOLON])
        new_list="$new_list `cat module_tmp_header.i | \
                  $SED -n 's/.*config_require(\(.*\)).*/\1/p'`"
        AC_MSG_MODULE_DBG(" $i will test: $new_list")
	if test "x$new_list" != "x"; then
	  for j in $new_list
	    do
            AC_MSG_MODULE_DBG(-n " $i wants to add $j: ")
	    if test ! -f $srcdir/$mibdir/$j.h -a ! -f $srcdir/$mibdir/$j.c; then
              AC_MSG_WARN([mib module error])
	      AC_MSG_ERROR([mib module "$i" requires module "$j" but $j.h or $j.c could not be found in $srcdir/$mibdir])
	    elif echo " $new_with_out_mib_modules " | $GREP " $j " > /dev/null; then
	      if test "x$i" != "xdefault_modules" ; then
                mib_cfg_msg="mib module '$i' requires module '$j' but someone told me to compile without it"
                AC_MSG_WARN([mib module error])
                if test "x$with_mib_cfg_checks" = "xyes"; then
                  AC_MSG_ERROR([$mib_cfg_msg])
                else
                  AC_MSG_WARN([$mib_cfg_msg])
                  AC_MSG_CACHE_ADD([*** MIB Module warning *** $mib_cfg_msg])
                fi
#	    elif echo " $module_list $new_list_two $new_module_list " | $GREP " $j " > /dev/null; then
#              AC_MSG_MODULE_DBG("  $j included previously")
              fi
	    else
	      new_list_two="$new_list_two $j"
              AC_MSG_MODULE_DBG("  $j included")
	    fi
	  done
	fi

        #---------------------
      	# macro: config_add_mib(SOME-MIB)
      	#  - lists MIBs to be added to the default to-load list.
        #
        # check if $i has any mibs to add
        #
        AH_TOP([#define config_add_mib(x) NETSNMP_REQUIRE_SEMICOLON])
        new_mibs=`cat module_tmp_header.i | \
                  $SED -n 's/.*config_add_mib(\(.*\)).*/\1/p'`
	if test "x$new_mibs" != "x"; then
	    for j in $new_mibs
		do
		if test "x`echo :$default_mibs: | $GREP :$j:`" = "x"; then
		    default_mibs="$default_mibs:$j"
		fi
		if test -f $srcdir/mibs/$j.txt; then
		    if echo $default_mibs_install | $GREP " $j " > /dev/null; then
			    :
		    else
			    default_mibs_install="$default_mibs_install $j.txt"
		    fi
		fi
	    done
	fi # new mibs

        #-------------------
        # add the current module to the list of stuff to compile, etc.
        #
        # add $i to module list
        #
        AC_MSG_MODULE_DBG("  $i added (based on $i.h)")
        module_list="$module_list $i"
        eval ${module_type}_list="\"\$${module_type}_list $i\""

	echo "read_config.lo: mibgroup/$i.h" >> $module_list_deps
	if test $module_type = "agent_module" ; then
 	    echo "snmp_vars.lo: mibgroup/$i.h" >> $agent_module_list_deps
        else
 	    echo "mib_modules.lo: mibgroup/$i.h" >> $mib_module_list_deps
        fi

        #-------------------
        # check for unsupported config_load_mib
        #
        if cat module_tmp_header.i | $GREP -q config_load_mib; then
          AC_MSG_WARN([mib module error])
      	  AC_MSG_WARN([mib module "$i" uses the "config_load_mib" directive, which is no longer supported.  It probably won't work.])
        fi # grep config_load_mib

        #-------------------
        # check for config_parse_dot_conf
        #  (generally not used any longer; old auto-load a .conf token)
        #
        AH_TOP([#define config_parse_dot_conf(w,x,y,z) NETSNMP_REQUIRE_SEMICOLON])
        changequote(, )
        cat module_tmp_header.i | \
        $SED -n 's@.*config_parse_dot_conf(\([^)]*\), *\([^),]*\), *\([^),]*\), *\([^),]*\)).*@register_config_handler("snmpd",\1, \2, \3, \4);@p' >> $mibdir/mib_module_dot_conf.h
	changequote([, ])

        #---------------------
      	# macro: config_error(error text)
      	#  - used to signal a configuration error from a module
        #
        # check if $i has any errors, or warnings
        #
        AH_TOP([#define config_error(x) NETSNMP_REQUIRE_SEMICOLON])
        error=`cat module_tmp_header.i | \
               $SED -n 's/.*config_error(\(.*\)).*/\1/p'`
	if test "x$error" != "x"; then
          echo
          echo
          AC_MSG_ERROR([$i: *** $error ***])
        fi

        #---------------------
      	# macro: config_warning(warning text)
      	#  - used to signal a configuration "warning" to be printed to the user
        #
        AH_TOP([#define config_warning(x) NETSNMP_REQUIRE_SEMICOLON])
        warning=`cat module_tmp_header.i | \
                 $SED -n 's/.*config_warning(\(.*\)).*/\1/p'`
	if test "x$warning" != "x"; then
          all_warnings="$all_warnings $warning
"
        fi

      #---------------------
      # Check for a .c file if the .h didn't exist
      #
      elif test -f $srcdir/$mibdir/$i.c; then
        if echo " $module_list $new_list_two $new_module_list " | $GREP " $i      " > /dev/null; then
           AC_MSG_MODULE_DBG("  $i already included")
        else
           AC_MSG_MODULE_DBG("  $i added (based on $i.c)")
	   module_list="$module_list $i"
	   mib_module_list="$mib_module_list $i"
        fi

      #---------------------
      # No .h or .c = error!  (typo?)
      #
      else # no $i.h or $i.c
        AC_MSG_WARN([mib module error])
	AC_MSG_ERROR([module files $i.h or $i.c could not be found in $srcdir/$mibdir])
      fi # no $i.h

      #-------------------
      # check for init/shutdown routines too
      #
      has_c_code=no
      if test -f $srcdir/$mibdir/$i.c; then
        if test "x$output_to" = "xnormal" ; then
	  echo "	$i \\" >> $module_list_code
  	  echo $ECHO_N " $i" $ECHO_C >> $MODULE_LIST
          eval tmpfilevar=\$`echo ${module_type}_list_code`
          echo "	$i \\" >> $tmpfilevar
        else
          eval "dl_${dl_name}_files=\"\$dl_${dl_name}_files $i\""
          saved_sublist_code="$saved_sublist_code $i"
        fi
        mnm=`echo $i | $SED 's/.*\///;s/\.c$//;'`
        AC_MSG_MODULE_DBG(-e "\nchecking '$srcdir/$mibdir/$i.c' ($mnm/`basename $i`) C file for init/shutdown")
        if test "x$mnm" != "x`basename $i`"; then
           AC_MSG_WARN([mib module error])
	   AC_MSG_ERROR([conflicting names: $mnm != `basename $i`])
        fi
        temp_init=`$GREP init_$mnm $srcdir/$mibdir/$i.c | $GREP -v _init_$mnm`
	if test "x$temp_init" != "x"; then
            AC_MSG_MODULE_DBG(-e "adding init_$mnm() to list")
            need_header=1
	    echo "  if (should_init(\"$mnm\")) init_$mnm();" >> $output_inits
	fi
	$GREP shutdown_$mnm $srcdir/$mibdir/$i.c > /dev/null
        if test $? -eq 0 ; then
            if test $module_debug -eq 1; then
              echo -e "\nadding shutdown_$mnm() to list"
            fi
            need_header=1
	    echo "  if (should_init(\"$mnm\")) shutdown_$mnm();" >> $mibdir/${module_type}_shutdown.h
	fi
      fi # test -f $srcdir/$mibdir/$i.c

      #-------------------
      # if there is a shutdown/init list,
      #
      if test $need_header -eq 1 ; then
         echo '#include "'"mibgroup/$i.h"'"' >> $output_hdrs
      else
         echo '/*#include "'"mibgroup/$i.h"'"*/' >> $output_hdrs
      fi

      #-------------------
      # check for output directory (for building outside of source tree
      #
      changequote(, )
      if test "x`echo $i | $SED 's/\///g;'`" != "x$i"; then
	mnd=`echo $i | $SED 's/\/[^/]*$//;'`
	if test ! -d ./$mibdir/$mnd; then
	    module_directories="$module_directories ./$mibdir/$mnd"
	fi
      fi # module directory test
      changequote([, ])

      #-------------------
      # define USING_*_MODULE
      #
      changequote(, )
      sym="`echo ${i} | $SED 's/[^a-zA-Z0-9_]/_/g' | tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'`"
      changequote([, ])
      echo "/* Define if compiling with the ${i} module files.  */" >> include/net-snmp/agent/${module_type}_config.h
      echo "#define USING_${sym}_MODULE 1" >> include/net-snmp/agent/${module_type}_config.h
      echo " " >> include/net-snmp/agent/${module_type}_config.h

      #-------------------
      echo $ECHO_N " $i$ECHO_C"
    fi #
    done # for i in $module_sublist

    if test "$output_to" = "separate" ; then
      if test "x$new_list_two" = "x" ; then
        # no new sub-modules to add to the current target
        output_to="normal"

        dllcleans="$saved_sublist_o $dllcleans"
        output_inits=""
        module_sublist=""
      else
        # a module had sub-modules required; keep goin'
        saved_sublist="$saved_sublist $module_sublist"
        module_sublist="$new_list_two"
        new_list_two=""
      fi
    else
      module_sublist=""
    fi

    done # while module_sublist
  done # for j in $new_module_list

  # remember the top level modules for later reporting
  if test $first_pass = 1 ; then
    # remember this list for later reporting
    top_level_modules="$new_module_list => $new_list_two"
    first_pass=0
  fi
  #-------------------
  # if $i added new modules vis config_*,
  # check those modules for requirements too
  #
  # transfer the new module to check list to the normal variable
  #
  if test "x$new_list_two" ; then
    output_to=normal
  fi
  new_module_list="$new_list_two"
  new_list_two=""
done # while test "x$new_module_list" != "x"; do
echo ""

#--------------------
# construct the dll code files and make rules.  This can't be done
# until now because duplicate use of a module means it gets removed
# from the dll and put into the main agent.
#
for i in $dl_names ; do
  eval mod_files="\"\$dl_${i}_files\""
  echo "components in the $i dll: $mod_files"

  output_inits="agent/mibgroup/${i}-dll.c"
  output_hdrs="agent/mibgroup/${i}-hdrs.h"

  AC_MSG_MODULE_DBG("creating dll file: $output_inits")
  for ofile in $output_inits $output_hdrs ; do
    if test -f $ofile ; then
      if $GREP "this file created automatically by configure" $ofile > /dev/null 2>&1 ; then
  	:
      else
  	AC_MSG_ERROR([$ofile already existed ; can't build dll object $i])
      fi
    fi
    cat > $ofile << EOFIF
/* xDo not edit; this file created automatically by configure */
EOFIF
  done

  cat >> $output_inits << EOFOC
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "${i}-hdrs.h"
void
init_$i(void) {
EOFOC

  # create the .so rule
  saved_sublist_o=`echo "$mod_files " | $SED 's/^ *//g;s/  */.o /g;'`

  # create the rules for each object
  for j in $mod_files ; do
    cat >> $module_rules << EOFR
$j.o: $j.c
	\$(CC) -c \$(CPPFLAGS) \$(CFLAGS) \$(DLFLAGS) -o \$@ \$<

EOFR
    mnm=`echo $j | $SED 's/.*\///;s/\.c$//;'`
    echo "  if (should_init(\"$mnm\")) init_$mnm();" >> $output_inits
  done

  # create the rule for the .so and wrapper code
  cat >> $module_rules << EOFM
${i}-dll.o: ${i}-dll.c
	\$(CC) -c \$(CPPFLAGS) \$(CFLAGS) \$(DLFLAGS) -o \$@ \$<

${i}.so: $saved_sublist_o ${i}-dll.o
	\$(CC) \$(CPPFLAGS) \$(CFLAGS) \$(DLFLAGS) -o \$@ $saved_sublist_o ${i}-dll.o

EOFM
        cat >> $output_inits << EOFOCFIN
}
EOFOCFIN

done

# cleanup
rm -f conftest.$ac_ext module_tmp_header.i

#-------------------
# build module lists for Makefiles
#
changequote(, )

for i in module_list_o module_list_c module_list_lo module_list_ft mib_module_list_o mib_module_list_c mib_module_list_lo mib_module_list_ft mibgroup_list_o mibgroup_list_lo mibgroup_list_ft agent_module_list_o agent_module_list_c agent_module_list_lo agent_module_list_ft agentgroup_list_o agentgroup_list_lo agentgroup_list_ft ; do
  eval "$i=mk/${i}.mk"
done

$SED 's/^module_list_code/module_list_o/;s/\([^=]\)  *\\/\1.o \\/g'  < $module_list_code  > $module_list_o
$SED 's/^module_list_code/module_list_c/;s/\([^=]\)  *\\/\1.c \\/g'  < $module_list_code  > $module_list_c
$SED 's/^module_list_code/module_list_lo/;s/\([^=]\)  *\\/\1.lo \\/g' < $module_list_code  > $module_list_lo
$SED 's/^module_list_code/module_list_ft/;s/\([^=]\)  *\\/\1.ft \\/g' < $module_list_code  > $module_list_ft

$SED 's/^mib_module_list_code/mib_module_list_o/;s/\([^=]\)  *\\/\1.o \\/g'  < $mib_module_list_code  > $mib_module_list_o
$SED 's/^mib_module_list_code/mib_module_list_c/;s/\([^=]\)  *\\/\1.c \\/g'  < $mib_module_list_code  > $mib_module_list_c
$SED 's/^mib_module_list_code/mib_module_list_lo/;s/\([^=]\)  *\\/\1.lo \\/g' < $mib_module_list_code  > $mib_module_list_lo
$SED 's/^mib_module_list_code/mib_module_list_ft/;s/\([^=]\)  *\\/\1.ft \\/g' < $mib_module_list_code  > $mib_module_list_ft

$SED 's/^mib_module_list_o/mibgroup_list_o/;s@\([^ 	]*\)\.o@mibgroup/\1.o@g' < $mib_module_list_o > $mibgroup_list_o
$SED 's/^mib_module_list_lo/mibgroup_list_lo/;s@\([^ 	]*\)\.lo@mibgroup/\1.lo@g' < $mib_module_list_lo > $mibgroup_list_lo
$SED 's/^mib_module_list_ft/mibgroup_list_ft/;s@\([^ 	]*\)\.ft@mibgroup/\1.ft@g' < $mib_module_list_ft > $mibgroup_list_ft

$SED 's/^agent_module_list_code/agent_module_list_o/;s/\([^=]\)  *\\/\1.o \\/g'  < $agent_module_list_code  > $agent_module_list_o
$SED 's/^agent_module_list_code/agent_module_list_c/;s/\([^=]\)  *\\/\1.c \\/g'  < $agent_module_list_code  > $agent_module_list_c
$SED 's/^agent_module_list_code/agent_module_list_lo/;s/\([^=]\)  *\\/\1.lo \\/g' < $agent_module_list_code  > $agent_module_list_lo
$SED 's/^agent_module_list_code/agent_module_list_ft/;s/\([^=]\)  *\\/\1.ft \\/g' < $agent_module_list_code  > $agent_module_list_ft

$SED 's/^agent_module_list_o/agentgroup_list_o/;s@\([^ 	]*\)\.o@mibgroup/\1.o@g' < $agent_module_list_o > $agentgroup_list_o
$SED 's/^agent_module_list_lo/agentgroup_list_lo/;s@\([^ 	]*\)\.lo@mibgroup/\1.lo@g' < $agent_module_list_lo > $agentgroup_list_lo
$SED 's/^agent_module_list_ft/agentgroup_list_ft/;s@\([^ 	]*\)\.ft@mibgroup/\1.ft@g' < $agent_module_list_ft > $agentgroup_list_ft

for i in module_list_o module_list_c module_list_lo module_list_ft mib_module_list_o mib_module_list_c mib_module_list_lo mib_module_list_ft mibgroup_list_o mibgroup_list_lo mibgroup_list_ft agent_module_list_o agent_module_list_c agent_module_list_lo agent_module_list_ft agentgroup_list_o agentgroup_list_lo agentgroup_list_ft ; do
	# hpux make (at least) doesn't like a trailing \ on the last
	# line even when the next line contains nothing but
	# whitespace.
	lasttoken=`awk '{lasttoken=$1}END{print lasttoken}' mk/$i.mk`
	$SED "s#$lasttoken \\\\#$lasttoken#" < mk/$i.mk > mk/$i.mk.tmp
	mv mk/$i.mk.tmp mk/$i.mk

	# add a closing comment
	echo "" >> mk/$i.mk
	echo "# end configure generated code" >> mk/$i.mk
done

changequote([, ])

if test $module_debug -eq 1; then
   echo -e "\nFinal module list: $module_list_code"
   exit
fi
if test "x$with_mib_cfg_checks" = "xyes"; then
  AC_MSG_CACHE_ADD([Agent Module list: $module_list_code])
fi
AC_SUBST_FILE(MODULE_LIST)
AC_SUBST_FILE(module_list_c)
AC_SUBST_FILE(module_list_o)
AC_SUBST_FILE(module_list_lo)
AC_SUBST_FILE(module_list_ft)
AC_SUBST_FILE(mib_module_list_c)
AC_SUBST_FILE(mib_module_list_o)
AC_SUBST_FILE(mib_module_list_lo)
AC_SUBST_FILE(mib_module_list_ft)
AC_SUBST_FILE(agent_module_list_c)
AC_SUBST_FILE(agent_module_list_o)
AC_SUBST_FILE(agent_module_list_lo)
AC_SUBST_FILE(agent_module_list_ft)
AC_SUBST_FILE(mibgroup_list_o)
AC_SUBST_FILE(mibgroup_list_lo)
AC_SUBST_FILE(mibgroup_list_ft)
AC_SUBST_FILE(agentgroup_list_o)
AC_SUBST_FILE(agentgroup_list_lo)
AC_SUBST_FILE(agentgroup_list_ft)

AC_SUBST(default_mibs_install)
AC_SUBST(dllcleans)
AC_SUBST_FILE(module_rules)
AC_SUBST_FILE(module_list_deps)
AC_SUBST_FILE(mib_module_list_deps)
AC_SUBST_FILE(agent_module_list_deps)

AC_MSG_RESULT(.)

if test $module_debug -eq 1; then
AC_MSG_CACHE_ADD(Agent MIB code:            $module_list)
fi
AC_MSG_CACHE_ADD(Agent MIB code:            $top_level_modules)

# end hairy agent module processing

AC_MSG_CHECKING([if directories need to be created])
for i in $module_directories; do
  $srcdir/mkinstalldirs $i
done
AC_MSG_RESULT(.)

#-------------------
# Define default mibs
#
AC_MSG_CHECKING([default mib files to read])
if test "x$NETSNMP_DEFAULT_MIBS" = "x"; then
  NETSNMP_DEFAULT_MIBS="$default_mibs"
fi
if test "x$ENV_SEPARATOR" != "x"; then
  :
elif test "x$PARTIALTARGETOS" = "xmingw32" -o "x$PARTIALTARGETOS" = "xmingw32msvc"; then
  # mingw32 uses ';' as the default environment variable separator
  ENV_SEPARATOR=";"
  NETSNMP_DEFAULT_MIBS=`echo "$NETSNMP_DEFAULT_MIBS" | $SED 's/:/;/g'`
  default_mibs=`echo "$default_mibs" | $SED 's/:/;/g'`
else
  ENV_SEPARATOR=":"
fi
AC_SUBST(ENV_SEPARATOR)
AC_DEFINE_UNQUOTED([ENV_SEPARATOR], ["$ENV_SEPARATOR"],
         [Environment separator character surrounded by double quotes.])
AC_DEFINE_UNQUOTED([ENV_SEPARATOR_CHAR], ['$ENV_SEPARATOR'],
          [Environment separator character surrounded by single quotes.])
AC_DEFINE_UNQUOTED(NETSNMP_DEFAULT_MIBS,"$NETSNMP_DEFAULT_MIBS")
AC_SUBST(NETSNMP_DEFAULT_MIBS)
AC_MSG_RESULT($NETSNMP_DEFAULT_MIBS)

#-------------------
# Define symbols for conditional compilation in makefiles
#

if echo " $agent_module_list " | \
   $FGREP ' agentx/agentx_config ' | \
   $FGREP ' agentx/protocol ' > /dev/null ; then
   NETSNMP_HAVE_AGENTX_LIBS_TRUE=''
   NETSNMP_HAVE_AGENTX_LIBS_FALSE='#'
else
   NETSNMP_HAVE_AGENTX_LIBS_TRUE='#'
   NETSNMP_HAVE_AGENTX_LIBS_FALSE=''
fi
AC_SUBST([NETSNMP_HAVE_AGENTX_LIBS_TRUE])
AC_SUBST([NETSNMP_HAVE_AGENTX_LIBS_FALSE])
